# encoding: UTF-8

class ApplicationController < ActionController::Base
  protect_from_forgery

  #helper_method :current_user
  #helper_method :user_signed_in?

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def user_signed_in?
    #return true if current_user
    if !current_user
    redirect_to signin_url, alert: "you must login!"
    else
      return true
    end
  end

	def render_404
    render_optional_error_file(404)
  end

  def render_403
    render_optional_error_file(403)
  end

  def render_optional_error_file(status_code)
    status = status_code.to_s
    if ["404","403", "422", "500"].include?(status)
      render :template => "/errors/#{status}", :format => [:html], :handler => [:erb], :status => status, :layout => "application"
    else
      render :template => "/errors/unknown", :format => [:html], :handler => [:erb], :status => status, :layout => "application"
    end
  end

	def notice_success(msg)
    flash[:notice] = msg
  end

  def notice_error(msg)
    flash[:notice] = msg
  end

  def set_seo_meta(title = '',meta_keywords = '', meta_description = '')
    if title.length > 0
      @page_title = "#{title}"+"_龍翔密传官网_凯乐斯网路"
    else
      @page_title = "龍翔密传官网_凯乐斯网络"
    end
    @meta_keywords = meta_keywords
    @meta_description = meta_description
  end

	def redirect_referrer_or_default(default)
    redirect_to(request.referrer || default)
  end

end
